//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using OpenLS.Spreadsheet.Formats.Biff.Records.Styling;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class BiffStyleRecord : WorkbookLevelRecord
    {
        public static BiffStyleRecord Create (CellStyle style, BiffWriterWorkbookContext context)
        {
            var result = new BiffStyleRecord();
            result._xfId = context.StyleStyleRecordTable[style.Record];
            if (style.IsBuiltIn)
            {
                result.IsBuiltIn = true;
                result._builtInId = style.BuiltInId.Value;
            }
            else
            {
                result._name = (XLUnicodeString) style.Name;
            }
            return result;
        }
        private ushort _xfId;
        private bool _isBuiltIn;
        private byte _builtInId;
        private byte _rowColumnLevel;
        private XLUnicodeString _name;

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.Style;}
        }

        public bool IsBuiltIn
        {
            get {
                return _isBuiltIn;
            }
            private set
            {
                _isBuiltIn = value;
            }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            ushort i = (ushort) _xfId;
            if (_isBuiltIn)
                i |= 0x8000;
            context.WriteUInt16(i);
            if (_isBuiltIn)
            {
                context.Write(_builtInId);
                context.Write(_rowColumnLevel);
            }
            else
            {
                _name.Write(context);
            }
        }
        protected override void ReadContent(BiffReaderContext context, int length)
        {
            ushort i = context.ReadUInt16();

            _xfId = (ushort) (i & (ushort)0xfff);
            _isBuiltIn = (i & 0x8000) != 0;
            if (_isBuiltIn)
            {
                _builtInId = context.ReadByte();
                this._rowColumnLevel = context.ReadByte();
            }
            else
            {
                _name.Read(context.Reader);
            }
#if DEBUG
            string s = (string) _name;
            if (s == "Bad")
            {
                i = i;
            }
#endif
        }
        public override void Apply(BiffReaderWorkbookContext context)
        {
            CellStyle style;
            StyleExtRecord styleExtRecord = Next as StyleExtRecord;
            byte? builtInId = null;
            if (IsBuiltIn)
                builtInId = this._builtInId;
            else if (styleExtRecord != null)
            {
                builtInId = styleExtRecord.BuiltInId;
            }
            if (builtInId != null)
            {
                style = new CellStyle();
                style.SetBuiltInId(builtInId.Value, context.Workbook);
            }
            else
            {
                style = new CellStyle((string) _name);
            }
           //\\ context.Workbook.StyleSheet.Styles.Add(style);
            var styleFormattingRecord = context.GetStyleFormattingRecord(_xfId);
            if (styleFormattingRecord == null)
            {
                // Contract.Assert(false);
                return;
            }
            if (styleFormattingRecord.Style != null)
            {
                var newStyleFormattingRecord = new StyleFormattingRecord();
                newStyleFormattingRecord.CopyFrom(styleFormattingRecord);
                styleFormattingRecord = newStyleFormattingRecord;
            }
                style.Record = styleFormattingRecord;
            context.Workbook.StyleSheet.Styles.Add(style);
            Contract.Assert(Enumerable.Any(context.Workbook.Styles, s => s == style));

//            style.Record = (StyleFormattingRecord) context.Workbook.StyleSheet.AddStyleRecord(styleFormattingRecord);

        }
    }
}